package com.example.shoppings.activity;

import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.shoppings.R;
import com.example.shoppings.adapter.ProductsAdapter;
import com.example.shoppings.sql.Products;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hasee on 2017-05-10.
 */

public class MainActivity extends ActionBarActivity {


    @BindView(R.id.tl_custom)
    Toolbar tlCustom;
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;
    @BindView(R.id.lv_left)
    ListView lvLeft;
    @BindView(R.id.drawlayout)
    DrawerLayout drawlayout;
    private View leftHeardView;

    private ActionBarDrawerToggle mDrawerToggle;
    private ArrayAdapter<String> adapter;
    private ProductsAdapter productsAdapter;
    private String [] lvs={"我的订单","退出"};

    private List<Products> productses;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        loadData();//加载数据库数据
        SQLiteDatabase db = Connector.getDatabase();
        setToolBar();
        mDrawerToggle=new ActionBarDrawerToggle(this,drawlayout,tlCustom,R.string.app_name
        ,R.string.app_name){
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };
        mDrawerToggle.syncState();
        drawlayout.addDrawerListener(mDrawerToggle);
        setLeftView();
//        DataSupport.deleteAll(Products.class);
//        setProductOnewData(1);
//        setProductTwowData(2);
    }

    Handler mhandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                Log.d("shop","UI更新");
                LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                productsAdapter=new ProductsAdapter(MainActivity.this,productses);
                rvProduct.setLayoutManager(layoutManager);
                rvProduct.setAdapter(productsAdapter);
            }
        }
    };

    private void loadData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                productses = DataSupport.findAll(Products.class);
                Message msg=Message.obtain();
                msg.what=1;
                mhandler.sendMessage(msg);
            }
        }).start();
    }


    private void setProductOnewData(int position){
        Products products=new Products();
        products.setProId(position);
        products.setProName("FLYTE");
        products.setProMsg("Inspired by Swedish minimalist colour palette," +
                " FLYTE Magnus is our lightest wood edition. ");
        products.setProPrice("2800");
        products.setProDetail(R.string.proDetail1);
        List<String> commentlist=new ArrayList<>();
        commentlist.add("so great");
        products.setComments(commentlist);
        products.setProSKU("small,big");
        List<Integer> imgP1=new ArrayList<>();
        imgP1.add(R.mipmap.main21);
        imgP1.add(R.mipmap.main22);
        products.setImgMainPath(imgP1);

        List<Integer> imgP2=new ArrayList<>();
        imgP2.add(R.mipmap.main22);
        products.setImgDetailPath(imgP2);
        products.setProNum("10");
        products.save();
    }

    private void setProductTwowData(int position){
        Products products=new Products();
        products.setProId(position);
        products.setProName("Sense Sleep System");
        products.setProMsg("Sense by Hello is a simple system that tracks your sleep behavior," +
                " monitors the environment of your bedroom and reinvents the alarm.. ");
        products.setProPrice("1520");
        List<String> commentlist=new ArrayList<>();
        commentlist.add("so great");
        products.setComments(commentlist);
        products.setProSKU("white,black");
        List<Integer> imgP1=new ArrayList<>();
        imgP1.add(R.mipmap.main1);
        imgP1.add(R.mipmap.main2);
        imgP1.add(R.mipmap.main3);
        products.setImgMainPath(imgP1);

        List<Integer> imgP2=new ArrayList<>();
        imgP2.add(R.mipmap.pro11);
        products.setImgDetailPath(imgP2);
        products.setProNum("10");
        products.save();
    }



    private void setToolBar(){
        tlCustom.setTitleTextColor(Color.WHITE);
        tlCustom.setTitle("Shop");
        setSupportActionBar(tlCustom);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void setLeftView() {
        if (leftHeardView == null) {
            leftHeardView = LayoutInflater.from(this).inflate(R.layout.head_left_layout, null);
        }
        lvLeft.addHeaderView(leftHeardView);
        lvLeft.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lvs));
        lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }
}
